package cn.ihoway.room;

import cn.ihoway.annotation.Processor;

import cn.ihoway.common.CommonProcessor;
import cn.ihoway.entity.Allocation;
import cn.ihoway.entity.Room;
import cn.ihoway.redis.AllocationRedis;
import cn.ihoway.redis.RoomRedis;
import cn.ihoway.room.io.RoomAddInput;
import cn.ihoway.room.io.RoomAddOutput;
import cn.ihoway.type.StatusCode;
import cn.ihoway.util.HowayEncrypt;
import cn.ihoway.util.HowayLog;
import cn.ihoway.util.HowayWsResult;

import java.util.Date;
import java.util.UUID;


/**
 * 创建新的房间
 * 系统随机生成房间号和默认配置
 */
@Processor(name = "RoomAddProcessor")
public class RoomAddProcessor extends CommonProcessor<RoomAddInput, RoomAddOutput> {
    private final HowayLog logger = new HowayLog(RoomAddProcessor.class);
    private final RoomRedis roomRedis = new RoomRedis();
    private final AllocationRedis allocationRedis = new AllocationRedis();

    @Override
    protected StatusCode dataCheck(RoomAddInput input){
        return StatusCode.SUCCESS;
    }

    @Override
    protected HowayWsResult process(RoomAddInput input, RoomAddOutput output) {
        //随机生成ROOM_ID
        String roomId = UUID.randomUUID().toString();
        roomId = HowayEncrypt.encrypt(roomId,10,"MD5",6);
        //创建新的房间
        Room room = new Room();
        room.setCreateTime(getCurrentTime(new Date(System.currentTimeMillis())));
        room.setCurrentNum(1);
        room.setHomeowner("1");
        room.setId(roomId);
        //以下是默认配置，房主可以进行修改
        room.setMaxNum(8);
        room.setMinNum(4); //设置的最小人数需要大于卧底数量
        room.setKnowOther(1);
        room.setMoleNum(2);
        room.setStatus(0);
        roomRedis.put(roomId,room);
        //房主加入房间
        Allocation allocation = new Allocation();
        allocation.setPlayerId("1");
        allocation.setRoomId(roomId);
        allocation.setSessionId(input.inChomm.sessionId);
        allocation.setIsAlive(1);
        allocation.setIsOnline(1);
        allocation.setPlayerType(1);
        allocation.setIsReady(0);
        allocationRedis.update(allocation);
        logger.info("创建房间" + roomId + "成功");
        output.roomId = roomId;
        return HowayWsResult.createSuccessResult(output);
    }
}